package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.DataFormatException;

/**
 * 菜品管理
 */
@Api(tags = "菜品管理")
@RequestMapping("/admin/dish")
@Slf4j
@RestController
public class DishController {
    @Autowired
    private DishService dishService;


    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> searchDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询{}", dishPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(dishService.searchDish(dishPageQueryDTO));
        log.info("开始查询记录数");
        Long count = dishService.searchCount(dishPageQueryDTO);
        log.info(String.valueOf(count));
        pageResult.setTotal(count);
        return Result.success(pageResult);
    }


    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> TypeDishSearch(Integer id) {
        List<Dish> dish = new ArrayList<>();
        dish = dishService.getTypeDish(id);
        return Result.success(dish);
    }



    /**
     * 根据ID查询菜品
     *
     * @param id
     * @return
     */
    @ApiOperation("根据ID查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getIdDish(@PathVariable Integer id) {
        DishVO dish = new DishVO();
        dish = dishService.getIdDish(id);
        return Result.success(dish);
    }


    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping()
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品{}", dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }


    /**
     * 启用状态设置
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("启用状态设置")
    @PostMapping("/status/{status}")
    public Result qi(@PathVariable short status, Long id) {
        log.info("启售状态设置{}{}", status, id);
        dishService.qi(status, id);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @DeleteMapping()
    public Result shanchu(Integer ids[]) {
        log.info("批量删除{}", ids);
        dishService.shan(ids);
        return Result.success();
    }


    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping()
    public Result xiugai(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品{}", dishDTO);
        dishService.xiugai(dishDTO);
        return Result.success();
    }

}
